package com.kfm.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.Room;
import com.kfm.hotel.domain.RoomType;
import com.kfm.hotel.service.RoomService;
import com.kfm.hotel.service.RoomTypeService;
import com.kfm.hotel.service.SysDictService;
import com.kfm.hotel.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService service;
    @Autowired
    private RoomTypeService roomTypeService;

    @Autowired
    private SysDictService sysDictService;

    @GetMapping
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("room/index");
        return modelAndView;
    }

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer limit,
                  Room entity) {
        PageInfo<Room> pageInfo = service.list(entity, page, limit);
        return R.ok(pageInfo);
    }

    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView("room/add");
        // 房间类型
        modelAndView.addObject("roomTypes", roomTypeService.getRoomTypes());
        // 楼层
        modelAndView.addObject("floors", sysDictService.getByType(2));
        return modelAndView;
    }

    @PostMapping("/add")
    public R add(@RequestBody Room room) throws Exception {

        boolean flag = service.save(room);

        if (flag) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @GetMapping("/get/{id}")
    public R getById(@PathVariable Integer id){
        return R.ok(service.getByIdDeep(id));
    }
}
